package com.example.neutonote2.data.db;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.TypeConverters;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.example.neutonote2.data.dao.CategoryDao;
import com.example.neutonote2.data.dao.NoteDao;
import com.example.neutonote2.data.dao.TagDao;
import com.example.neutonote2.data.model.Category;
import com.example.neutonote2.data.model.Note;
import com.example.neutonote2.data.model.NoteTagCrossRef;
import com.example.neutonote2.data.model.Tag;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * NeuToNote应用的Room数据库
 */
@Database(entities = {Note.class, Tag.class, Category.class, NoteTagCrossRef.class}, version = 1, exportSchema = false)
@TypeConverters({DateConverter.class})
public abstract class NeuToNoteDatabase extends RoomDatabase {

    public abstract NoteDao noteDao();
    public abstract TagDao tagDao();
    public abstract CategoryDao categoryDao();

    private static volatile NeuToNoteDatabase INSTANCE;
    private static final int NUMBER_OF_THREADS = 4;
    public static final ExecutorService databaseWriteExecutor =
            Executors.newFixedThreadPool(NUMBER_OF_THREADS);

    public static NeuToNoteDatabase getDatabase(final Context context) {
        if (INSTANCE == null) {
            synchronized (NeuToNoteDatabase.class) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                            NeuToNoteDatabase.class, "neutónote_database")
                            .addCallback(sRoomDatabaseCallback)
                            .build();
                }
            }
        }
        return INSTANCE;
    }

    /**
     * 预填充回调，用于在数据库首次创建时添加默认分类
     */
    private static final RoomDatabase.Callback sRoomDatabaseCallback = new RoomDatabase.Callback() {
        @Override
        public void onCreate(@NonNull SupportSQLiteDatabase db) {
            super.onCreate(db);

            databaseWriteExecutor.execute(() -> {
                // 添加默认分类
                CategoryDao categoryDao = INSTANCE.categoryDao();
                
                Category work = new Category("工作", "work", true);
                Category study = new Category("学习", "school", true);
                Category life = new Category("生活", "home", true);
                
                categoryDao.insert(work);
                categoryDao.insert(study);
                categoryDao.insert(life);
                
                // 添加默认标签
                TagDao tagDao = INSTANCE.tagDao();
                
                Tag important = new Tag("重要", "#FF5722");
                Tag review = new Tag("待复习", "#3F51B5");
                Tag idea = new Tag("创意", "#009688");
                
                tagDao.insert(important);
                tagDao.insert(review);
                tagDao.insert(idea);
            });
        }
    };
} 